/*
@hehung
email: 1398660197@qq.com
wechat: hehung95
reproduced and please indicate the source @hehung
*/


#ifndef APP_WIFI_ESP8266_H_
#define APP_WIFI_ESP8266_H_

#include "app_common.h"


/* ESP8266 delay in milliseconds between AT command retry */
#define ESP8266_DELAY_0      (0U)       ///< No delay
#define ESP8266_DELAY_20MS   (20U)      ///< Delay of 20 milliseconds
#define ESP8266_DELAY_50MS   (50U)      ///< Delay of 50 milliseconds
#define ESP8266_DELAY_100MS  (100U)     ///< Delay of 100 milliseconds
#define ESP8266_DELAY_200MS  (200U)     ///< Delay of 200 milliseconds
#define ESP8266_DELAY_300MS  (300U)     ///< Delay of 300 milliseconds
#define ESP8266_DELAY_500MS  (500U)     ///< Delay of 500 milliseconds
#define ESP8266_DELAY_1000MS (1000U)    ///< Delay of 1000 milliseconds
#define ESP8266_DELAY_2000MS (2000U)    ///< Delay of 2000 milliseconds
#define ESP8266_DELAY_3000MS (3000U)    ///< Delay of 3000 milliseconds
#define ESP8266_DELAY_4000MS (4000U)    ///< Delay of 4000 milliseconds
#define ESP8266_DELAY_5000MS (5000U)    ///< Delay of 5000 milliseconds

/* ESP8266 retry count for AT command set */
#define ESP8266_RETRY_VALUE_0    (0U)       ///< No Retry
#define ESP8266_RETRY_VALUE_1    (1U)       ///< Retry Once
#define ESP8266_RETRY_VALUE_3    (3U)       ///< Retry 3 times
#define ESP8266_RETRY_VALUE_5    (5U)       ///< Retry 5 times
#define ESP8266_RETRY_VALUE_10   (10U)      ///< Retry 10 times

/** Logical Value TRUE for Cellular */
#define SF_WIFI_TRUE        (1U)
/** Logical Value FALSE for Cellular */
#define SF_WIFI_FALSE       (0U)


#define BUFFER_SIZE          (1024)

typedef struct
{
    char  *p_cmd;                // AT Command
    uint8_t  *p_success_resp;       // Success response string
    uint16_t  max_resp_length;       // Maximum length of expected response
    uint8_t   retry;                  // Retry count
    uint16_t  retry_delay;           // Delay between AT command retry
    uint16_t  wait_timeout;          // Waiting time for feedback
} esp8266_at_cmd_set_t;

typedef struct
{
    char     at_buf[BUFFER_SIZE];
    uint16_t at_len;
} esp8266_at_rx_t;

typedef enum
{
    ESP8266_ATCMD_IDX_STAMODE = 0,
	ESP8266_ATCMD_IDX_ATE0,
	ESP8266_ATCMD_IDX_RESET,
	ESP8266_ATCMD_IDX_APMODE,
    ESP8266_ATCMD_IDX_APSTAMODE,
    ESP8266_ATCMD_IDX_HOTSPOT_FD, 	
    ESP8266_ATCMD_IDX_HOTSPOT,
    ESP8266_ATCMD_IDX_SINGLELINKMODE,
    ESP8266_ATCMD_IDX_MULTILINKMODE,
    ESP8266_ATCMD_IDX_SERVER_CONN,
    ESP8266_ATCMD_IDX_TRANSMODE,
    ESP8266_ATCMD_IDX_SENDDATA,
	ESP8266_ATCMD_IDX_SNTPCFG,
	ESP8266_ATCMD_IDX_GETSNTPTIME,
	ESP8266_ATCMD_IDX_MQTTUSERCFG,
	ESP8266_ATCMD_IDX_MQTTCONN,
	ESP8266_ATCMD_IDX_MQTTPUB,
	ESP8266_ATCMD_IDX_MQTTSUB
} esp8266_at_cmd_index_t;


//void Esp8266_Test(void);
//void Esp8266_Init(void);
//void Esp8266_SetStationMode ( void );
//void ESP8266_SetApMode ( void );
//void Esp8266_SetStationAndApMode ( void );
//void Esp8266_StationConnect( char * id ,  char * password , uint8_t timeout );
//void Esp8266_SetLinkMode( uint8_t mode );
//void Esp8266_ServerConnect( char * server_id ,  char * port , uint8_t timeout );
//void Esp8266_SetTransparentMode( void );
//void Esp8266_SetSendData( void );

extern void Esp8266_Init(void);
extern fsp_err_t Esp8266_ATCommandExe(esp8266_at_cmd_index_t at_cmd, 
                                uint8_t *resp_buff);
extern fsp_err_t Esp8266_MqttPost(s_postParamType *post_param);
extern fsp_err_t Esp8266_MqttSubscriber(void);


#endif /* APP_WIFI_ESP8266_H_ */
